package cn.wenhe9.novel.service.impl;

import cn.wenhe9.novel.domain.dto.ImgVerifyCodeDto;
import cn.wenhe9.novel.manager.VerifyCodeManager;
import cn.wenhe9.novel.result.ResultResponse;
import cn.wenhe9.novel.service.ResourceService;
import cn.wenhe9.novel.util.ImgUploadUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 资源（图片/视频/文档）相关服务实现类
 *
 * @author DuJinliang
 * 2022/5/18
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Resource
    private VerifyCodeManager verifyCodeManager;

    @Override
    public ResultResponse<ImgVerifyCodeDto> getImgVerifyCode() throws IOException {
        String sessionId = IdWorker.get32UUID();
        return ResultResponse.ok(ImgVerifyCodeDto.builder()
                .sessionId(sessionId)
                .img(verifyCodeManager.genImgVerifyCode(sessionId))
                .build());
    }

    @Override
    public ResultResponse<String> uploadImg(MultipartFile file) {
        String url = ImgUploadUtils.upload(file);
        return ResultResponse.ok(url);
    }

}
